package com.sanploy.manage.service.impl;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sanploy.common.conf.PubliceVariable;
import com.sanploy.common.domain.AttrClass;
import com.sanploy.common.domain.GoodsAttr;
import com.sanploy.common.domain.vo.GoodsAttrClassVo;
import com.sanploy.common.enums.Meta;
import com.sanploy.common.vo.BaseException;

import com.sanploy.manage.mapper.GoodsAttrMapper;
import com.sanploy.manage.service.GoodsAttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

/**
 * Created by yingzhi zhu on 2017/9/17.
 * @author yingzhi zhu
 * 商品规格的接口逻辑处理类
 */
@Service
public class GoodsAttrServiceImpl implements GoodsAttrService {


    @Autowired
    private GoodsAttrMapper goodsAttrMapper;


    private Gson gson = new Gson();

    @Override
    public int insertGoodsAttr(String goodsAttrsJson, Long goodsId) {
        //解析json数据并设置商品id 后添加进数据库
        List<GoodsAttr> goodsAttrList = gson.fromJson(goodsAttrsJson, new TypeToken<List<GoodsAttr>>() {
        }.getType());
        goodsAttrList.forEach(goodsAttr -> {
            goodsAttr.setGoods_id(goodsId);
        });
        if (goodsAttrList.size()>0){
            goodsAttrMapper.insertAttr(goodsAttrList);
        }
        return PubliceVariable.SuccessfulInt;
    }

    @Override
    public int updateGoodsAttr(String goodsAttrsJson, Long goodsId) {
        //解析json数据并设置商品id 后修改
        List<GoodsAttr> goodsAttrList = gson.fromJson(goodsAttrsJson, new TypeToken<List<GoodsAttr>>() {
        }.getType());
        goodsAttrList.forEach(goodsAttr -> {
            if (goodsAttr.getId() == null) {
                goodsAttr.setGoods_id(goodsId);
                goodsAttrMapper.insertOne(goodsAttr);
            } else {
                goodsAttrMapper.updateAttr(goodsAttr);
            }
        });
        return PubliceVariable.SuccessfulInt;
    }

    @Override
    public int updateGoodsAttr(Long id, GoodsAttr goodsAttr, ServletRequest servletRequest) throws BaseException {
        if (goodsAttrMapper.updateAttr(goodsAttr) < PubliceVariable.MinEffectRow) {
            throw new BaseException(Meta.UpdateFail);
        }
        return PubliceVariable.SuccessfulInt;
    }


    //TODO 改进这段糟糕的代码


    @Override
    public List<GoodsAttrClassVo> findGoodsAttrByGoodsId(Long goodsId) {
        //糟糕的代码 主要用于查找出来商品规格 并且对商品规格按照商品规格分类来进行排序
        List<GoodsAttr> list = goodsAttrMapper.findGoodsAttrByGoodsId(goodsId);
        Function<List<GoodsAttr>, List<GoodsAttrClassVo>> function = (p) -> {
            String attrClass = "";
            if (p.size()>0) {
                attrClass = p.get(0).getAttr_class_object().getName();
            }
            List<GoodsAttrClassVo> goodsAttrClassVos = new ArrayList<>();

            GoodsAttrClassVo goodsAttrClassVo = null;
            goodsAttrClassVo = new GoodsAttrClassVo();
            goodsAttrClassVo.setGoodsAttrList(new ArrayList<>());
            for (GoodsAttr goodsAttr : p) {
                //等于时 首先
                if (!attrClass.equals(goodsAttr.getAttr_class_object().getName())) {
                    goodsAttrClassVos.add(goodsAttrClassVo);
                    goodsAttrClassVo = new GoodsAttrClassVo();
                    goodsAttrClassVo.setGoodsAttrList(new ArrayList<>());
                    attrClass = goodsAttr.getAttr_class_object().getName();
                }


                goodsAttrClassVo.setAttrName(attrClass);
                goodsAttrClassVo.getGoodsAttrList().add(goodsAttr);


            }
            if (p.size()>0) {
                goodsAttrClassVos.add(goodsAttrClassVo);
            }

            return goodsAttrClassVos;
        };


        return function.apply(list);
    }

    //查找数组类型参数
    @Override
    public List<GoodsAttr> findAttrArr(String[] item_goods_attr_id) {
        return goodsAttrMapper.findAttrArr(item_goods_attr_id);
    }

    @Override
    public GoodsAttr findOneAttr(Long id) {
        return goodsAttrMapper.findGoodsAttrOne(id);
    }

    @Override
    public int insertGoodsAttrClass(String name, int sort) throws BaseException {
        if (goodsAttrMapper.insertGoodsAttrClass(name, sort) < PubliceVariable.MinEffectRow) {
            throw new BaseException(Meta.InsertFail);
        } else {
            return PubliceVariable.SuccessfulInt;
        }
    }

    @Override
    public List<AttrClass> findAllAttrClass() {
        return goodsAttrMapper.findAttrClassAll();
    }

    @Override
    public AttrClass findOneClass(Long id) {
        return goodsAttrMapper.findAttrClassOne(id);
    }

    @Override
    public int updateAttrClassName(String name, int sort_order, Long id) throws BaseException {
        if (goodsAttrMapper.updateAttrClassOne(name, sort_order, id) < PubliceVariable.MinEffectRow) {
            throw new BaseException(Meta.UpdateFail);
        }
        return PubliceVariable.SuccessfulInt;
    }

}
